package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.Articleinfo;
import com.example.demo.entity.BlogRecycle;
import com.example.demo.entity.Userinfo;
import com.example.demo.service.ArticleService;
import com.example.demo.service.BlogRecycleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/recycle")
public class BlogRecycleController {

    @Autowired
    private BlogRecycleService blogRecycleService;
    
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/recyclelist")
    public AjaxResult getMyList(HttpServletRequest request){
        Userinfo userinfo = UserSessionUtils.getUser(request);
        if(userinfo == null){
            return AjaxResult.fail(-1,"非法请求");
        }

        List<BlogRecycle> list = blogRecycleService.getlist(userinfo.getId());
        
        //自己实现的
        //对于 content  的设置。
        for (BlogRecycle blogRecyle:list) {
            blogRecyle.setContent(blogRecyle.getContent().replaceAll("#",""));
            blogRecyle.setContent(blogRecyle.getContent().replaceAll("%",""));
            blogRecyle.setContent(blogRecyle.getContent().replaceAll("## ",""));
            if(blogRecyle.getContent().length()>15){
                blogRecyle.setContent(blogRecyle.getContent().substring(0,9));
            }
        }

        return AjaxResult.success(list);
    }

    @RequestMapping("/restore")
    public AjaxResult restore(HttpServletRequest request,Integer id) {
        if(id == null || id <= 0){
            //参数有误
            return AjaxResult.fail(-1,"参数异常");
        }

        Userinfo userinfo = UserSessionUtils.getUser(request);
        if(userinfo == null){
            return AjaxResult.fail(-1,"非法请求");
        }

        BlogRecycle blogRecycle = blogRecycleService.getBlogRecycleById(id);
        Articleinfo article = new Articleinfo();

        article.setId(id);
        article.setUid(userinfo.getId());
        article.setCreatetime(blogRecycle.getCreatetime());
        article.setUpdatetime(blogRecycle.getUpdatetime());
        article.setTitle(blogRecycle.getTitle());
        article.setContent(blogRecycle.getContent());
        article.setRcount(blogRecycle.getRcount());
        article.setState(blogRecycle.getState());

        blogRecycleService.delete(id,userinfo.getId());
        return AjaxResult.success(articleService.add(article));
    //    if(blogRecycle == null){
    }

    @RequestMapping("/deleteForever")
    public AjaxResult deleteForever(HttpServletRequest request,Integer id) {
        if(id == null || id <= 0){
            //参数有误
            return AjaxResult.fail(-1,"参数异常");
        }
        Userinfo userinfo = UserSessionUtils.getUser(request);
        if(userinfo == null){
            return AjaxResult.fail(-1,"非法请求");
        }

        return AjaxResult.success(blogRecycleService.deleteForever(id,userinfo.getId()));
    }

}
